// pages/downloading/downloading.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        totalSize: 0,
        downSize: 0,
        progress: 0,
    },
    task: null, // 下载任务    
    listener: function (res) { // 订阅下载进度函数
        this.setData({
            progress: res.progress,
            downSize: res.totalBytesWritten,
            totalSize: res.totalBytesExpectedToWrite
        })
    },
    downloadFile() {
        console.log('开始下载')
        // 获取到下载任务
        this.task = wx.downloadFile({
            url: 'https://zukisuzukibgm.com/download/heart/',
            success(res) {
                if (res.statusCode === 200) {
                    wx.playVoice({
                        filePath: res.tempFilePath,
                    })
                }
            }
        })
        this.task.onProgressUpdate(this.listener)
    },
    abortTask() {
        if (!!this.task) {
            this.task.offProgressUpdate(this.listener)
            this.task.abort()
            this.task = null
        }
        this.setData({
            progress: 0,
            downSize: 0,
            totalSize: 0
        })
    },

    uploadFile() {
        wx.chooseImage({
            count: 1
        }).then(res => {
            console.log(res)
            wx.uploadFile({
                filePath: res.tempFilePaths[0],
                name: 'file',
                url: 'http://10.99.151.45:8080/file/upload',
                success: (res => {
                    console.log('上传成功')
                    console.log(res)
                })
            })
        })
    }
})